import {Component} from '@angular/core';
import {InfiniteScroll, IonicPage, NavController, NavParams, PopoverController} from 'ionic-angular';
import {PagingConfig} from "@providers/paging";
import {StorageProvider} from "@providers/storage";
import {DialogProvider} from "@providers/dialog";
import {WorkerProvider} from "@providers/interface/worker";
import {Util} from "@providers/util";

/**
 * Generated class for the WorkerCompletionRecordPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-worker-completion-record',
  templateUrl: 'worker-completion-record.html',
})
export class WorkerCompletionRecordPage {

  datas: any[] = [];
  userInfo: any = this.storageService.read("userInfo");
  datasConfig: PagingConfig = new PagingConfig();
  time: any = '7';
  timeStarts: string = Util.dateToString(new Date(new Date().setDate(new Date().getDate() - 7)));
  timeEnds: string = Util.dateToString(new Date());
  max: string = Util.dateToString(new Date());

  constructor(public navCtrl: NavController,
              public popoverCtrl: PopoverController,
              public navParams: NavParams,
              private storageService: StorageProvider,
              private dialog: DialogProvider,
              private worker: WorkerProvider) {
  }

  ionViewDidLoad() {
    this.dialog.showLoading();
    this.getData(data => {
      this.datas = data;
    }, this.datasConfig);
  }

  push(page: string, data?: any) {
    this.navCtrl.push(page, data);
  }

  /**
   * 获取数据
   * @param pagingConfig 分页
   * @param fn 方法
   */
  private getData(fn: (value) => void, pagingConfig) {
    this.worker.getProductNumSkip(
      {"WORKER": this.userInfo["PARTYID"], 'time': this.time , 'time_end': this.timeEnds}
      , pagingConfig).then((data) => {
      this.dialog.hideLoading();
      for (let i = 0; i < data.length; i++) {
        console.log(data[i].RETURNED);
        if (data[i].RETURNED == "1970-01-01 00:00:00") {
          data[i].RETURNED = "";
        }
      }
      fn(data);
    }).catch((error) => {
      this.dialog.showAlert(error);
    });
  }

  /**
   * 下拉刷新
   * @param infiniteScroll
   */
  getMoreDatas(infiniteScroll: InfiniteScroll) {
    this.getData((value) => {
      this.datas.push.apply(this.datas, value);
      infiniteScroll.complete();
    }, this.datasConfig);
  }

  //打开条件弹窗
  presentPopover(myEvent) {
    let popover = this.popoverCtrl.create('SelectPopoverPage', {'time': this.time}, {cssClass: 'order_list_page'});
    popover.onDidDismiss(data => {
      if (data != null) {
        if (data != '0') {
          this.time = data
        } else {
          this.time = null;
          this.timeStarts = '';
          this.timeEnds = '';
        }
        this.datasConfig = new PagingConfig();
        this.getData(data => {
          console.log(JSON.stringify(data));
          this.datas = data;
        }, this.datasConfig);
      }
    });

    popover.present({
      ev: myEvent
    });
  }

  //按照回执时间查询完工工单
  timeQuery() {
    this.worker.getProductNumSkip(
      {"WORKER": this.userInfo["PARTYID"], 'STARTTIME': this.timeStarts, 'ENDTIME': this.timeEnds}, new PagingConfig()
    ).then((data) => {
      this.dialog.hideLoading();
      for (let i = 0; i < data.length; i++) {
        if (data[i].RETURNED == "1970-01-01 00:00:00") {
          data[i].RETURNED = "";
        }
      }
      this.datas = data;
    }).catch(e => {
      this.dialog.showAlert(e);
    });
  }
}
